package com.liuqing.www.java基础知识.网络通信;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

public class InetAddress详细 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
			getInetAddress();
			getURLAddress();
	}
	public static void getInetAddress(){
		try {
			InetAddress adress = InetAddress.getByName("www.baidu.com");
			System.out.println(adress.getHostName());//输出www.baidu.com
			System.out.println(adress.getHostAddress());//输出61.135.169.125
			//<!--****此方法速度较慢*****/
			InetAddress adress2=InetAddress.getByAddress(new byte[]{(byte)61,(byte)135,(byte)169,(byte)125});
			System.out.println(adress2.getHostName());//输出www.baidu.com
			System.out.println(adress2.getHostAddress());//输出61.135.169.125
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void getURLAddress(){
		try {
			URL url=new URL("http://news.baidu.com/ns?word=%B0%B5%CA%BE%B7%A8&tn=news&from=news&cl=2&rn=20&ct=1");
			System.out.println("url:"+url.getFile());
			//<!--这样的写法也可以与服务器交互，但一般用(HttpURLConnection)url.openConnection()，这样可以设置客户端的信息
			//con.getOutputStream(),con.getInputStream()
			BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));//与网络资源建立关联，这样可以
			while(br.readLine()!=null){
				System.out.println(br.readLine());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
